package org.linlinjava.litemall.admin.web.distribution;

import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.DisMemberAmount;
import org.linlinjava.litemall.db.domain.DisMemberInfo;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.distribution.DisMemberAmountService;
import org.linlinjava.litemall.db.service.distribution.DisMemberInfoService;
import org.linlinjava.litemall.db.util.DateTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员账户管理控制器
 *
 * @author huangpu
 * @Date 2018-05-02 19:21:08
 */
@Controller
@RequestMapping("/admin/disMemberAmount")
public class DisMemberAmountController extends BaseController {


    @Autowired
    DisMemberAmountService disMemberAmountService;

    @Autowired
    DisMemberInfoService disMemberInfoService;
    @Autowired
    LitemallUserService userService;

    /**
     * 获取会员账户管理列表
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public Object list(@LoginAdmin Integer adminId,
                       String username, String userId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<DisMemberAmount> list = disMemberAmountService.selectList(username, userId, page, limit);
        int total = disMemberAmountService.countSeletive(username, userId, page, limit);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);
    }


    /**
     * 新增会员账户管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody LitemallUser user) {

        String now = DateTools.getCurrentSysData(DateTools.FULLTIME_FORMAT);
        try {
            disMemberInfoService.createMember(user.getId());
            user.setUserLevel("经销商用户");
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    /**
     * 删除会员账户管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {

        return null;
    }


    /**
     * 修改会员账户管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return null;
    }

    /**
     * 会员账户管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
